package com.liug.droidsmall2.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by LG on 2018/9/29.
 */

public class di extends View {

    private Paint paint;
    private PaintFlagsDrawFilter drawFilter;
    int bigRadius = 10;
    int smallRadius = 0;
    int distance = 10;
    int width = 10;
    int height = 10;
    int fraction = 10;
    int currentDis = 10;
    private Path path;

    public di(Context context) {
        super(context);
    }

    public di(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public di(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private void init(Context context, AttributeSet attrs) {
        drawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG
                | Paint.FILTER_BITMAP_FLAG);
        paint = new Paint();
        path = new Path();
        paint.setColor(Color.GRAY);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        // 转换为像素单位
/*        bigRadius = dip2px(context, bigRadius);
        smallRadius = dip2px(context, smallRadius);
        distance = dip2px(context, distance);*/
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 必须重新new,不然路径会重复，我之前就是这样

        // 把画布移到中心
//        canvas.translate(width / 2, height / 2);
        // 从canvas层面去除绘制时锯齿
//        canvas.setDrawFilter(drawFilter);
        // 当前的两个圆心的距离
        currentDis = distance * fraction;
        // 计算当前大圆的半径

//        path.moveTo(0, 0);
//        path.rQuadTo(100, -150, 200, 0);
//        path.rQuadTo(100, 150, 200, 0);
//        path.lineTo(100, 400);
                canvas.drawPath(path, paint);

        // 用二阶贝塞尔曲线画左边边的曲线，参数的第一个点是左边的一个控制点

        // 画大圆
  /*      canvas.drawCircle(0, 0, bigRadius, paint);
        // 画小圆
        canvas.drawCircle(0, currentDis, smallRadius, paint);*/
        // 画path
//        canvas.drawPath(path, paint);

/*
        float bigRadius = this.bigRadius - currentDis / bigPercent;
        float smallRadius = 0;
        if (currentDis > 5) {// 距离大于5才改变小圆的半径
            smallRadius = this.smallRadius - currentDis / smallPercent;
        }
        // 大圆两边的两个点坐标
        leftX = -bigRadius;// 大圆当前的半径
        leftY = rightY = 0;
        rightX = bigRadius;// 大圆当前的半径
        // 小圆两边的两个点坐标
        leftX2 = -smallRadius;// 小圆当前的半径
        leftY2 = rightY2 = currentDis;
        rightX2 = -leftX2; // 小圆当前的半径
        // 两个控制点
        controlX1 = -smallRadius;// x坐标取小圆当前的半径大小
        controlY1 = currentDis / 2;// y坐标取两个圆距离的一半
        controlX2 = smallRadius;// x坐标取小圆当前的半径大小
        controlY2 = currentDis / 2;// y坐标取两个圆距离的一半
        path.moveTo(leftX, leftY);
        path.lineTo(rightX, rightY);
        // 用二阶贝塞尔曲线画右边的曲线，参数的第一个点是右边的一个控制点
        path.quadTo(controlX2, controlY2, rightX2, rightY2);
        path.lineTo(leftX2, leftY2);
        // 用二阶贝塞尔曲线画左边边的曲线，参数的第一个点是左边的一个控制点
        path.quadTo(controlX1, controlY1, leftX, leftY);
        // 画大圆
        canvas.drawCircle(0, 0, bigRadius, paint);
        // 画小圆
        canvas.drawCircle(0, currentDis, smallRadius, paint);
        // 画path
        canvas.drawPath(path, paint);
*/

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(event.getX(), event.getY());
                return true;

            case MotionEvent.ACTION_MOVE:
                path.lineTo(event.getX(), event.getY());
                postInvalidate();    // 重新绘制
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }
}
